package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.activity.dao.tbl_TripItems;
import com.rc.activity.dao.tbl_TripItemsDao;
import com.rc.net.NetSupport;
import com.rc.object.BaseObject;

public class TripItemsSync {

	private Context context;
	private SQLiteDatabase db;
	private DaoSession daoSession;
	public String query;
	private tbl_TripItemsDao daoTripItems;
	public Cursor cursor;
	public static String[] keys = { "status", "TripItemID", "messages" };

	public TripItemsSync(Context context, SQLiteDatabase db,
			DaoSession daoSession) {
		this.context = context;
		this.daoSession = daoSession;
		this.db = db;
		daoTripItems = this.daoSession.getTbl_TripItemsDao();
	}

	/**
	 * Delete multi TripItems
	 */
	public void deleteTripItems() {
		new callDeleteTripItems().execute("");
	}

	/**
	 * Insert TripItems
	 */
	public void insertTripItems() {
		new callInsertTripItems().execute("");
	}

	/**
	 * Update TripItems
	 */
	public void updateTripItems() {
		new callUpdateTripItems().execute("");
	}

	/**
	 * call multidelteTripItems
	 */
	private class callDeleteTripItems extends
			AsyncTask<String, Void, ArrayList<BaseObject>> {
		ArrayList<tbl_TripItems> arrTripItems;

		public callDeleteTripItems() {
			super();
		}

		@Override
		protected ArrayList<BaseObject> doInBackground(String... params) {
			// The string name is query, providing that "IsCreate" = 1
			query = tbl_TripItemsDao.Properties.IsDelete.columnName + " = '1'";

			// get data in sqlite
			cursor = db
					.query(daoTripItems.getTablename(),
							daoTripItems.getAllColumns(), query, null, null,
							null, null);
			arrTripItems = new ArrayList<tbl_TripItems>();

			// get ArrayList Item by cursor
			arrTripItems = tbl_TripItems.getObjectbyCursor(cursor);
			if (arrTripItems.size() > 0) {
				ArrayList<Long> arrTripItem = new ArrayList<Long>();
				for (tbl_TripItems objTripItem : arrTripItems) {
					arrTripItem.add(objTripItem.getTripItemID());
				}
				// query server
				if (arrTripItem != null) {
					if (arrTripItem.size() > 0) {
						NetSupport.deleteTripItem(arrTripItem, context);
					}
				}

			}
			return null;

		}

		protected void onPostExecute(ArrayList<BaseObject> arrTripItemsID) {
			insertTripItems();
		}
	}

	/**
	 * call multiInsertTripItems
	 */

	private class callInsertTripItems extends
			AsyncTask<String, Void, ArrayList<BaseObject>> {
		ArrayList<tbl_TripItems> arrTripItems;

		@Override
		protected ArrayList<BaseObject> doInBackground(String... params) {
			// The string name is query, providing that "IsCreate" = 1
			query = tbl_TripItemsDao.Properties.IsCreate.columnName + " = '1'";

			// get data in sqlite
			cursor = db
					.query(daoTripItems.getTablename(),
							daoTripItems.getAllColumns(), query, null, null,
							null, null);
			arrTripItems = new ArrayList<tbl_TripItems>();

			// get ArrayList Item by cursor
			arrTripItems = tbl_TripItems.getObjectbyCursor(cursor);
			if (arrTripItems.size() > 0) {

				// query server
				return NetSupport.insertTripItem(arrTripItems);
			}
			return null;

		}

		protected void onPostExecute(ArrayList<BaseObject> arrBase) {
			if (arrBase != null) {
				for (int i = 0; i < arrBase.size(); i++) {
					if (arrBase.get(i).getParam(keys[0]).equals("success")) {
						arrTripItems.get(i).setIsCreate("");
						db.update(
								tbl_TripItemsDao.TABLENAME,
								arrTripItems.get(i).getContentValues(),
								tbl_TripItemsDao.Properties.TripItemID.columnName
										+ " = "
										+ arrTripItems.get(i).getTripItemID(),
								null);
					} else {

					}
				}

			}
			updateTripItems();
		}
	}

	/**
	 * call update trip items
	 */
	private class callUpdateTripItems extends
			AsyncTask<String, Void, ArrayList<tbl_TripItems>> {

		@Override
		protected ArrayList<tbl_TripItems> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_TripItemsDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_TripItems> arrTripItems;
			ArrayList<Long> arrTripItemsID = new ArrayList<Long>();

			// get data in sqlite
			cursor = db
					.query(daoTripItems.getTablename(),
							daoTripItems.getAllColumns(), query, null, null,
							null, null);
			arrTripItems = new ArrayList<tbl_TripItems>();

			// get ArrayList TripItems by cursor
			arrTripItems = tbl_TripItems.getObjectbyCursor(cursor);
			if (arrTripItems.size() > 0) {
				for (tbl_TripItems objTripItems : arrTripItems) {
					// get ID TripItems
					arrTripItemsID.add(objTripItems.getTripItemID());
				}
				// query server
				if (NetSupport.updateTripItems(arrTripItems, context)) {
					return arrTripItems;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_TripItems> arrTripItemsID) {
			if (arrTripItemsID != null) {
				for (tbl_TripItems objTripItems : arrTripItemsID) {
					objTripItems.setIsUpdate("");
					int row = db.update(daoTripItems.getTablename(),
							objTripItems.getContentValues(),
							tbl_TripDao.Properties.TripID.columnName + " = "
									+ objTripItems.getTripID(), null);
					Log.e("update_trip_item_success", row + "");
				}
			}
		}
	}
}
